﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Editing;
using System;
using System.Collections.Generic;

namespace Blueprints.Editor
{
    /// <summary>
    /// Contains extension methods for the view provider.
    /// </summary>
    public static class ViewProviderExtensions
    {
        /// <summary>
        /// Filters properties by EditType.
        /// </summary>
        /// <typeparam name="ViewType">The type of view.</typeparam>
        /// <param name="properties">The properties to filter.</param>
        /// <param name="types">The EditTypes to return.</param>
        /// <returns>All properties that match <paramref name="types"/>.</returns>
        public static IEnumerable<PropertyView<ViewType>> Filter<ViewType>(this IEnumerable<PropertyView<ViewType>> properties, params EditType[] types)
        {
            foreach (PropertyView<ViewType> property in properties)
            {
                if (Array.IndexOf(types, property.Property.EditType) >= 0)
                {
                    yield return property;
                }
            }
        }

        /// <summary>
        /// Sorts properties by name.
        /// </summary>
        /// <typeparam name="ViewType">The type of view.</typeparam>
        /// <param name="properties">The properties to sort.</param>
        /// <returns>The properties, as a sorted list.</returns>
        public static IReadOnlyList<PropertyView<ViewType>> Sort<ViewType>(this IEnumerable<PropertyView<ViewType>> properties)
        {
            List<PropertyView<ViewType>> list = new List<PropertyView<ViewType>>(properties);
            list.Sort((left, right) => 
            {
                return string.Compare(left.Property.Name, right.Property.Name, StringComparison.OrdinalIgnoreCase);
            });

            return list;
        }
    }
}
